package com.hrt.manage.sequence.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.Cash;
import com.hrt.bean.Comments;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.GiftType;
import com.hrt.bean.OrderInfor;
import com.hrt.bean.ProType;
import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.Resources;
import com.hrt.bean.Role;
import com.hrt.bean.Sequence;
import com.hrt.bean.Stocks;
import com.hrt.bean.ViewResource;
import com.hrt.manage.sequence.dao.SequenceDao;
import com.hrt.manage.sequence.dao.impl.SequenceDaoImpl;
import com.hrt.manage.sequence.service.SequenceService;
import com.hrt.util.WebParams;

@Service

public class SequenceServiceImpl implements SequenceService {

	@Autowired
	private SequenceDao dao;

	

	@Override
	@Transactional
	public String getNextSequence(Class clz) {
		Sequence seq=dao.getSequence(clz);
		 Long maxId=seq.getMaxId();
		 String nextId=new String();
		
		if(maxId==null ||maxId<0)
		{
			maxId=0l;
			nextId="1";
		}else{
			nextId=String.valueOf(maxId+1);
		}
		seq.setMaxId(maxId+1);
		dao.updateEntity(seq);
		String seqCode=GenarateSeqCode(maxId+1,seq);
		return seqCode;
	}

	/**
	 * 生成编码方法
	 * @param nextId
	 * @param seq
	 * @return
	 */
	private  String GenarateSeqCode(Long nextId, Sequence seq) {
		
		String prefix= seq.getPrefix();
		Long seqNum=seq.getNums();
		String tableName=seq.getTableName();
		
		StringBuffer seqNumStr=new StringBuffer();
		String seqStr=new String();
		int numLen=nextId.toString().length();
		for(int i=0;i<seqNum.intValue();i++){
			seqNumStr.append("0");
		}
		int cutLen=seqNumStr.length();
		if(numLen<seqNum)
		{
			 cutLen=seqNum.intValue()-numLen;
			 seqStr= seqNumStr.substring(0, cutLen);
			 seqStr=seqStr+nextId.toString();
		}
		if(tableName!=null && !"".equals(tableName))
		{
			if(tableName.equals(OrderInfor.class.getName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMdd");
				String seqCode=mf.format(date);
				seqCode=prefix+seqCode+seqStr.toString();
				return seqCode;
			}
			
			if(tableName.equals(Stocks.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMdd");
				String stockDate=mf.format(date);
				String stockCode=prefix+stockDate+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(GiftInfor.class.getSimpleName()))
			{
				String stockCode=prefix+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(GiftDetail.class.getSimpleName()))
			{
				String stockCode=prefix+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(ProductInfor.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMM");
				String seqCode=mf.format(date);
				String stockCode=seqCode+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(ProductDetails.class.getSimpleName()))
			{
				String stockCode="R"+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(ProType.class.getSimpleName()))
			{
				String stockCode=prefix+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(GiftType.class.getSimpleName()))
			{
				String stockCode=prefix+seqStr.toString();
				return stockCode;
			}
			if(tableName.equals(OrderInfor.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMddHHmmss");
				String seqCode=mf.format(date);
				seqCode=prefix+seqCode+seqStr.toString();
				return seqCode;
			}
			if(tableName.equals(Resources.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMddHHmmss");
				String seqCode=mf.format(date);
				seqCode=prefix+seqCode+seqStr.toString();
				return seqCode;
			}
			if(tableName.equals(Cash.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMddHHmmss");
				String seqCode=mf.format(date);
				seqCode=prefix+seqCode+seqStr.toString();
				return seqCode;
			}
			if(tableName.equals(Comments.class.getSimpleName()))
			{
				Date date=new Date();
				SimpleDateFormat mf=new SimpleDateFormat("yyyyMMddHHmmss");
				String seqCode=mf.format(date);
				seqCode=prefix+seqCode+seqStr.toString();
				return seqCode;
			}
			if(tableName.equals(Role.class.getSimpleName()))
			{
				
				String seqCode=prefix+seqStr.toString();
				return seqCode;
			}
			if(tableName.equals(ViewResource.class.getSimpleName()))
			{
				
				String seqCode=prefix+seqStr.toString();
				return seqCode;
			}
		}
	
		return null;
		
		
		
	
		
	}

	
	
	
	
	
	@Override
	public List<Sequence> queryByPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		List<Sequence>  list=new ArrayList<Sequence>();
		list=dao.queryByPage( pageIndex,  pageSize);
		return list;
	}

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		return dao.totalCount();
	}

	@Override
	@Transactional
	public void deleteById(Long[] ids) throws Exception {
		// TODO Auto-generated method stub
		if(ids!=null && ids.length>0){
		for(Long id:ids){
			Sequence  seq=dao.getById(id);
			seq.setIsdelete(new Integer(1));
			dao.updateEntity(seq);
		}
		}
		
	}

	@Override
	public void saveSequence(Sequence seq) throws Exception {
		// TODO Auto-generated method stub
		dao.saveEntity(seq);
		
	}

	@Override
	public void updateSequence(Sequence seq) {
		// TODO Auto-generated method stub
		dao.updateEntity(seq);
		
	}

	@Override
	public Sequence getSequenceByid(Long seqId) {
		// TODO Auto-generated method stub
		return dao.getById(seqId);
	}

	@Override
	public Sequence getSequenceByEntity(Class clz) {
		// TODO Auto-generated method stub
		return dao.getSequence(clz);
	}


	
	
	

}
